﻿using FastCloud.Base;
using FastCloud.Base.DB;
using FastCloud.Base.Entity;
using FastIOC.Attribute;
using FastORM.Entity;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.Base
{
    [Route("frame/base/attachconfig")]
    public class AttachConfigController : BaseController
    {
        [AutoWired]
        private DB_AttachConfig DB_AttachConfig;
        [AutoWired]
        private DB_AttachInfo DB_AttachInfo;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/Base/AttachConfig/AttachCongfig_List.cshtml");
        }
        [Route("add")]
        public IActionResult Add()
        {
            return View("~/Views/Base/AttachConfig/AttachCongfig_Add.cshtml");
        }
        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/Base/AttachConfig/AttachCongfig_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string ConfigName = ValueConvert.ConvertToString(GetFormParam("ConfigName"));
            List<ParamEntity> ParamList = new List<ParamEntity>();
            string sql = "1=1";
            if (!string.IsNullOrEmpty(ConfigName))
            {
                sql += " and ConfigName like @ConfigName";
                ParamList.Add(new ParamEntity() { ParamName = "ConfigName", ParamValue = "%" + ConfigName + "%" });
            }
            return Result.OK(DB_AttachConfig.SelectPage("*", sql, "SortNum desc", ParamList));
        }

        [Route("opinsert"), HttpPost]
        public IActionResult OpInsert(Frame_AttachConfig Entity)
        {
            if (DB_AttachConfig.ExistConfig(Entity.ConfigName))
            {
                return Result.Error("已存在相同名称的数据库连接！");
            }
            else
            {
                DB_AttachConfig.Insert(Entity);
                return Result.OK();
            }
        }

        [Route("opupdate"), HttpPost]
        public IActionResult OpUpdate(Frame_AttachConfig Entity)
        {
            if (DB_AttachConfig.ExistConfig(Entity.ConfigGuid, Entity.ConfigName))
            {
                return Result.Error("已存在相同名称的数据库连接！");
            }
            else
            {
                DB_AttachConfig.UpdateByID(Entity);
                return Result.OK();
            }
        }

        [Route("opdetail"), HttpPost]
        public IActionResult OpDetail()
        {
            string ConfigGuid = ValueConvert.ConvertToString(GetFormParam("ConfigGuid"));
            return Result.OK(DB_AttachConfig.SelectOne(ConfigGuid));
        }

        [Route("opbatchdelete"), HttpPost]
        public IActionResult OpBatchDelete()
        {
            string KeyID = ValueConvert.ConvertToString(GetFormParam("KeyID"));
            bool IsHave = false;
            foreach (string KeyGuid in KeyID.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList())
            {
                Frame_AttachConfig Config = DB_AttachConfig.SelectOne(KeyGuid);
                List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = "ConfigName", ParamValue = Config.ConfigName } };
                string sql = string.Format("ConfigName=@ConfigName");
                int count = DB_AttachInfo.SelectCount(sql, ParamList);
                if (count>0)
                {
                    IsHave = true;
                }
            }
            if (!IsHave)
            {
                DB_AttachConfig.DeleteBatch(KeyID.TrimEnd(';').Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList());
            }
            else
            {
                return Result.Error("存在附件使用附件库连接，无法删除");
            }
            return Result.OK();
        }
    }
}
